package com.example.j20240317.demos.web.controller;


import com.example.j20240317.demos.web.constants.Constants;
import com.example.j20240317.demos.web.model.*;
import com.example.j20240317.demos.web.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/book")
public class BookController{

    @Autowired
    private BookService bookService;


    @RequestMapping("/addBook")
public String addBook(BookInfo bookInfo){
        log.info("添加图书，BookInfo:{}",bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName())
                ||!StringUtils.hasLength(bookInfo.getAuthor())
                ||!StringUtils.hasLength(bookInfo.getPublish())
                ||bookInfo.getCount()==null
                ||bookInfo.getPrice()==null
 //               ||bookInfo.getStatus() ==null
                       ){
            return "输入参数不合法，请检查参数输入！";
        }

        try{
            bookService.insertBook(bookInfo);
        }catch (Exception e){
            return "内部发生错误，请联系管理员";
        }
        return  "";
}
    @RequestMapping("/getListByPage")
    public Result getListByPage(PageRequest pageRequest, HttpSession httpSession) {
        log.info("查询列表信息，pageRequest:{}", pageRequest);

        UserInfo userInfo = (UserInfo) httpSession.getAttribute(Constants.USER_SESSION_KEY);
        if (userInfo == null || userInfo.getId() < 1) {
            return Result.unLogin();
        }
        if (pageRequest.getCurrentPage() < 1) {
            return Result.fail("非法参数");

        }
        PageResult<BookInfo> pageResult = bookService.getListByPage(pageRequest);
        return Result.success(pageResult);
    }


        @RequestMapping("/queryBookById")
        public BookInfo queryBookById(Integer bookId){
            log.info("查询图书信息，bookId:"+bookId);
            //参数校验;
            if(bookId<1){
                log.error("非法的图书Id,bookId:"+bookId);
                return null;
            }
            return bookService.queryBookById(bookId);
        }

@RequestMapping("/updateBook")
public boolean updateBook(BookInfo bookInfo) {
    log.info("更新图书信息，updateBook:{}", bookInfo);
    if (!StringUtils.hasLength(bookInfo.getBookName())
            || !StringUtils.hasLength(bookInfo.getAuthor())
            || !StringUtils.hasLength(bookInfo.getPublish())
            || bookInfo.getCount() == null
            || bookInfo.getPrice() == null
        //               ||bookInfo.getStatus() ==null
    ) {
        return false;
    }

    try {
        Integer result = bookService.updateBook(bookInfo);
        if (result <= 0) {
            return false;
        }
        //return true;
    } catch (Exception e) {
        log.error("更新图书失败,e:{}", e);
        return false;
    }
    return true;
}


    @RequestMapping("/batchDelete")
    public boolean batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除数据, ids:{}", ids);
        try {
            Integer result = bookService.batchDelete(ids);
            if (result<=0){
                return false;
            }
            return true;
        }catch (Exception e){
            log.error("批量删除数据失败, ids:{}, e:{}",ids, e);
            return false;
        }
    }


}

